package com.easy;

/**
 * Created by Lxk on 2019/11/3.
 */
public class Solution1089 {

    public void duplicateZeros(int[] arr) {
        int len = arr.length;
        int count = 0, posMark = 0;
        for (int i = 0; i < len; i++) {
            if (arr[i] == 0) {
                count += 2;
            } else {
                count += 1;
            }
            //判断当前长度是否大于容量
            if (count >= len) {
                posMark = i;
                break;
            }
        }
        int pos = len - 1;
        for (int i = posMark; (i >= 0) && (pos >= 0); i--) {
            if (arr[i] == 0 && i == posMark && count > len && pos >= 0) {
                arr[pos--] = 0;
            } else if (arr[i] == 0 && i == posMark && count == len && pos >= 0) {
                arr[pos--] = 0;
                if (pos >= 0) {
                    arr[pos--] = 0;
                }
            } else if (arr[i] == 0 && pos >= 0) {
                arr[pos--] = 0;
                if (pos >= 0) {
                    arr[pos--] = 0;
                }
            } else if (arr[i] != 0 && pos >= 0) {
                arr[pos--] = arr[i];
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 0, 2, 3, 0, 4, 5, 0};
        Solution1089 solution1089 = new Solution1089();
        solution1089.duplicateZeros(arr);
    }

}
